<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="基团编号" prop="groupNo">
        <el-input
          v-model="queryParams.groupNo"
          placeholder="请输入基团编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基团名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入基团名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="基团符号" prop="groupSymbol">
        <el-input
          v-model="queryParams.groupSymbol"
          placeholder="请输入基团符号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['constituent:group:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['constituent:group:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['constituent:group:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['constituent:group:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="groupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="基团编号" align="center" prop="groupNo" />
      <el-table-column label="基团名称" align="center" prop="groupName" />
      <el-table-column label="基团符号" align="center" prop="groupSymbol" />
      <el-table-column label="振动类型" align="center" prop="vibrationType" />
      <el-table-column label="波长范围/nm" align="center">
        <template slot-scope="scope">
          {{ scope.row.startWavelength }}~{{ scope.row.endWavelength }}
        </template>
      </el-table-column>
      <el-table-column label="波数范围/cm^-1" align="center">
        <template slot-scope="scope">
          {{ scope.row.startWavenumber }}~{{ scope.row.endWavenumber }}
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['constituent:group:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['constituent:group:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改基团数据对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="基团编号" prop="groupNo">
          <el-input v-model="form.groupNo" placeholder="请输入基团编号" />
        </el-form-item>
        <el-form-item label="基团名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入基团名称" />
        </el-form-item>
        <el-form-item label="基团符号" prop="groupSymbol">
          <el-input v-model="form.groupSymbol" placeholder="请输入基团符号" />
        </el-form-item>
        <el-form-item label="起始波长" prop="startWavelength">
          <el-input v-model="form.startWavelength" placeholder="请输入起始波长" />
        </el-form-item>
        <el-form-item label="振动类型" prop="vibrationType">
          <el-input v-model="form.vibrationType" placeholder="请输入起始波长" />
        </el-form-item>
        <el-form-item label="结束波长" prop="endWavelength">
          <el-input v-model="form.endWavelength" placeholder="请输入结束波长" />
        </el-form-item>
        <el-form-item label="起始波数" prop="startWavenumber">
          <el-input v-model="form.startWavenumber" placeholder="请输入起始波数" />
        </el-form-item>
        <el-form-item label="结束波数" prop="endWavenumber">
          <el-input v-model="form.endWavenumber" placeholder="请输入结束波数" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入是否删除" />
        </el-form-item>
        <el-form-item label="新增人id" prop="creatorId">
          <el-input v-model="form.creatorId" placeholder="请输入新增人id" />
        </el-form-item>
        <el-form-item label="新增人" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入新增人" />
        </el-form-item>
        <el-form-item label="修改人id" prop="modifierId">
          <el-input v-model="form.modifierId" placeholder="请输入修改人id" />
        </el-form-item>
        <el-form-item label="修改人" prop="modifier">
          <el-input v-model="form.modifier" placeholder="请输入修改人" />
        </el-form-item>
        <el-form-item label="修改时间" prop="modifyTime">
          <el-date-picker clearable
            v-model="form.modifyTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修改时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->

        <el-drawer
      id="dataFormDrawer"
      :title="title"
      :visible.sync="open"
      size="350px"
      style="height: 100vh"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="基团编号" prop="groupNo">
          <el-input v-model="form.groupNo" placeholder="请输入基团编号" :disabled="isEdit" />
        </el-form-item>
        <el-form-item label="基团名称" prop="groupName">
          <el-input v-model="form.groupName" placeholder="请输入基团名称" />
        </el-form-item>
        <el-form-item label="基团符号" prop="groupSymbol">
          <el-input v-model="form.groupSymbol" placeholder="请输入基团符号" />
        </el-form-item>
        <el-form-item label="振动类型" prop="vibrationType">
          <el-input v-model="form.vibrationType" placeholder="请输入振动类型" />
        </el-form-item>
        <el-form-item label="起始波长" prop="startWavelength">
          <el-input v-model="form.startWavelength" placeholder="请输入起始波长" />
        </el-form-item>
        <el-form-item label="结束波长" prop="endWavelength">
          <el-input v-model="form.endWavelength" placeholder="请输入结束波长" />
        </el-form-item>
        <el-form-item label="起始波数" prop="startWavenumber">
          <el-input v-model="form.startWavenumber" placeholder="请输入起始波数" />
        </el-form-item>
        <el-form-item label="结束波数" prop="endWavenumber">
          <el-input v-model="form.endWavenumber" placeholder="请输入结束波数" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div class="drawer-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { listGroup, getGroup, delGroup, addGroup, updateGroup } from "@/api/constituent/group";

export default {
  name: "Group",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 基团数据表格数据
      groupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否为编辑模式（用于控制基团编号是否可编辑）
      isEdit: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupNo: null,
        groupName: null,
        groupSymbol: null,
        vibrationType: null,
        startWavelength: null,
        endWavelength: null,
        startWavenumber: null,
        endWavenumber: null,
        remarks: null,
        isDelete: null,
        creatorId: null,
        creator: null,
        modifierId: null,
        modifier: null,
        modifyTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        groupNo: [
          { required: true, message: "基团编号不能为空", trigger: "blur" }
        ],
        groupName: [
          { required: true, message: "基团名称不能为空", trigger: "blur" }
        ],
        groupSymbol: [
          { required: true, message: "基团符号不能为空", trigger: "blur" }
        ],
        vibrationType: [
          { required: true, message: "振动类型不能为空", trigger: "change" }
        ],
        startWavelength: [
          { required: true, message: "起始波长不能为空", trigger: "blur" },
          { validator: this.validateStartWavelength, trigger: "blur" }
        ],
        endWavelength: [
          { required: true, message: "结束波长不能为空", trigger: "blur" },
          { validator: this.validateEndWavelength, trigger: "blur" }
        ],
        startWavenumber: [
          { required: true, message: "起始波数不能为空", trigger: "blur" },
          { validator: this.validateStartWavenumber, trigger: "blur" }
        ],
        endWavenumber: [
          { required: true, message: "结束波数不能为空", trigger: "blur" },
          { validator: this.validateEndWavenumber, trigger: "blur" }
        ],
        isDelete: [
          { required: true, message: "是否删除不能为空", trigger: "blur" }
        ],
        creatorId: [
          { required: true, message: "新增人id不能为空", trigger: "blur" }
        ],
        creator: [
          { required: true, message: "新增人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "新增时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 波长和波数验证方法
    validateStartWavelength(rule, value, callback) {
      if (value && this.form.endWavelength) {
        const start = parseFloat(value);
        const end = parseFloat(this.form.endWavelength);
        if (!isNaN(start) && !isNaN(end) && start > end) {
          callback(new Error('起始波长不能大于结束波长'));
        } else {
          // 重新验证结束波长字段
          if (this.form.endWavelength) {
            this.$refs.form.validateField('endWavelength');
          }
          callback();
        }
      } else {
        callback();
      }
    },
    validateEndWavelength(rule, value, callback) {
      if (value && this.form.startWavelength) {
        const start = parseFloat(this.form.startWavelength);
        const end = parseFloat(value);
        if (!isNaN(start) && !isNaN(end) && end < start) {
          callback(new Error('结束波长不能小于起始波长'));
        } else {
          callback();
        }
      } else {
        callback();
      }
    },
    validateStartWavenumber(rule, value, callback) {
      if (value && this.form.endWavenumber) {
        const start = parseFloat(value);
        const end = parseFloat(this.form.endWavenumber);
        if (!isNaN(start) && !isNaN(end) && start > end) {
          callback(new Error('起始波数不能大于结束波数'));
        } else {
          // 重新验证结束波数字段
          if (this.form.endWavenumber) {
            this.$refs.form.validateField('endWavenumber');
          }
          callback();
        }
      } else {
        callback();
      }
    },
    validateEndWavenumber(rule, value, callback) {
      if (value && this.form.startWavenumber) {
        const start = parseFloat(this.form.startWavenumber);
        const end = parseFloat(value);
        if (!isNaN(start) && !isNaN(end) && end < start) {
          callback(new Error('结束波数不能小于起始波数'));
        } else {
          callback();
        }
      } else {
        callback();
      }
    },
    /** 查询基团数据列表 */
    getList() {
      this.loading = true;
      listGroup(this.queryParams).then(response => {
        this.groupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.isEdit = false; // 重置编辑状态
      this.form = {
        groupId: null,
        groupNo: null,
        groupName: null,
        groupSymbol: null,
        vibrationType: null,
        startWavelength: null,
        endWavelength: null,
        startWavenumber: null,
        endWavenumber: null,
        remarks: null,
        isDelete: null,
        creatorId: null,
        creator: null,
        createTime: null,
        modifierId: null,
        modifier: null,
        modifyTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.groupId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.isEdit = false; // 新增模式，基团编号可编辑
      this.open = true;
      this.title = "添加基团数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.isEdit = true; // 编辑模式，基团编号不可编辑
      const groupId = row.groupId || this.ids
      getGroup(groupId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改基团数据";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.groupId != null) {
            updateGroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const groupIds = row.groupId || this.ids;
      this.$modal.confirm('是否确认删除基团数据编号为"' + groupIds + '"的数据项？').then(function() {
        return delGroup(groupIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('constituent/group/export', {
        ...this.queryParams
      }, `group_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
#dataFormDrawer .el-input {
  width: 95%; /* 输入框占满剩余宽度（匹配图片效果） */
}
.drawer-footer {
  position: absolute;
  right: 10px;
  bottom: 10px;
}
</style>